package com.neuedu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.neuedu.domain.Userinfo;
import com.neuedu.service.UserinfoService;
import com.neuedu.service.impl.UserinfoServiceImpl;
import com.neuedu.util.MD5;

/**
 * Servlet implementation class UserSetServlet
 */
@WebServlet("/user/set")
public class UserSetServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public UserSetServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String re_nowpass = request.getParameter("re_nowpass");
		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("login_user");
		String log_pass = userinfo.getPassword();
		if(log_pass.equals(MD5.getMD5(re_nowpass))) {			
			response.getWriter().print(0);
		}else {
			response.getWriter().print(1);
			
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserinfoService us = new UserinfoServiceImpl();
		String password = request.getParameter("L_pass");
		Userinfo user = (Userinfo)request.getSession().getAttribute("login_user");
		int id = user.getId();
		Userinfo userinfos = new Userinfo();
		userinfos.setId(id);
		userinfos.setPassword(MD5.getMD5(password));
		int count = us.updatepass(userinfos);
		if(count == 1){
			/*request.getSession().setAttribute("login_user", userinfos);*/
			user.setPassword(MD5.getMD5(password));
			response.getWriter().print(1);
		}
		if(count == 0) {
			response.getWriter().print(0);
		}
		
	}
	
}
